Imports System.Windows.Forms
Imports System.IO.Ports

Public Class dlgOptions
    Dim dt As New DataTable
    Dim sql As String
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Try
            
            setOptions("Print", cbprint.Checked * -1)
            setOptions("Order", chkAutoOrder.Checked * -1)
            setOptions("Port", txtPort.Text.Replace("'", "''"))
            setOptions("Header", txtHeader.Text.Replace("'", "''"))
            setOptions("Footer", TxtFooter.Text.Replace("'", "''"))
            setOptions("Color", rbRed.Checked * -1)
            setOptions("TotalOfDay", nuptotal.Value)
            setOptions("TimeOut", nupTimeout.Value)
            setOptions("Errors", cbDisplay.SelectedIndex)

            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()

            If dt.Rows.Count > 0 Then
                db.updateTable(dt, sql)
                'MsgBox("Saved!!!", MsgBoxStyle.Information)
            End If
        
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
    
    Private Sub dlgOptions_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbprint.Checked = getOptions("Print")
        chkAutoOrder.Checked = getOptions("Order")
        txtPort.Text = getOptions("Port")
        txtHeader.Text = getOptions("Header")
        TxtFooter.Text = getOptions("Footer")

        If getOptions("Color") = 0 Then
            rbBlack.Checked = True
        Else
            rbRed.Checked = True
        End If
        nuptotal.Value = getOptions("TotalOfday")
        nupTimeout.Value = getOptions("TimeOut")
        sql = "select eid as 'Error',ename as 'Name',edesc as 'Message' from errors"
        bindSQL(sql)
        cbDisplay.SelectedIndex = getOptions("Errors")
    End Sub


    Private Sub btntest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntest.Click
        Dim f As New POSPrinter.U220POS
        Dim color As Byte
        color = getOptions("Color")

        f.openPrinter(getOptions("Port"))
        f.WriteString(getOptions("Header") & Chr(13) & Chr(10), 0, 0, 1, color)
        f.WriteString("Testing...", 0, 0, 0, color)
        f.WriteString(getOptions("Footer"), 0, 0, 1, color)
        f.closePrinter()
    End Sub
    Private Sub bindSQL(ByVal sql As String)
        Dim bs As New BindingSource
        dt = db.getvalue(sql).Tables(0)
        bs.DataSource = dt
        dgvErrors.DataSource = bs

        For i As Integer = 0 To dt.Columns.Count - 1
            dgvErrors.Columns(i).Name = dgvErrors.Columns(i).HeaderText
        Next
        dgvErrors.Columns(0).ReadOnly = True
        dgvErrors.Columns(1).ReadOnly = True
        dgvErrors.AllowUserToDeleteRows = False
        dgvErrors.AllowUserToAddRows = False
        dgvErrors.Columns(0).Width = 50
        dgvErrors.Columns(1).Width = 100
        dgvErrors.Columns(2).Width = 300
        
    End Sub
End Class

